package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.enums.OperationTypeEnum;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.dto.CgnciOperationLogsDTO;
import com.zatech.cgnci.project.entity.CgnciOperationLogsDO;
import com.zatech.cgnci.project.mapper.CgnciOperationLogsMapper;
import com.zatech.cgnci.project.service.CgnciOperationLogsService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 操作日志表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2025-01-09
 */
@Service
public class CgnciOperationLogsServiceImpl extends ServiceImpl<CgnciOperationLogsMapper, CgnciOperationLogsDO> implements CgnciOperationLogsService {

    /**
     * 插入操作日志
     *
     * @param bizNo         业务号
     * @param bizType       业务场景类型(BusinessScenariosEnum)
     * @param operationType 操作类型（OperationTypeEnum）
     * @param content       内容
     * @param creator       创建人
     */
    @Override
    public void insertOperationLogs(String bizNo, String bizType, String operationType,
                                    String content, String creator) {
        this.baseMapper.insert(this.buildOperationLogsDO(bizNo, bizType, operationType, content, creator));
    }

    //审核意见
    @Override
    public void insertReviewOpinions(String bizNo, String bizType, String operationType, String content, String creator) {
        this.insertOperationLogs(bizNo, bizType, operationType, content, creator);
    }

    //备注
    @Override
    public void insertRemarks(String bizNo, String bizType, String operationType, String content, String creator) {
        this.insertOperationLogs(bizNo, bizType, operationType, content, creator);
    }



    @Override
    public void saveLog(CgnciOperationLogsDTO req) {
        Assert.notNull(req, "日志对象必填");
        Assert.hasText(req.getBizNo(), "业务号必填。");
        Assert.hasText(req.getCreator(), "创建人必填。");
        Assert.hasText(req.getOperationType(), "日志操作类型必填。");
        this.baseMapper.insert(ConvertUtils.convert(req, CgnciOperationLogsDO.class));
    }

    /**
     * 根据业务号和业务类型查询操作日志
     */
    @Override
    public List<CgnciOperationLogsDTO> queryByBizNo(String bizNo, String bizType) {
        Assert.hasText(bizNo, "业务号不可为空");
        Assert.hasText(bizType, "业务类型不可为空");
        List<CgnciOperationLogsDTO> operationLogs = this.baseMapper.queryOperationLogs(bizNo, bizType);
        operationLogs.forEach(i->{
            i.setOperationTypeName(OperationTypeEnum.getEnumDescByCode(i.getOperationType()));
        });
        return operationLogs;
    }

    private CgnciOperationLogsDO buildOperationLogsDO(String bizNo, String bizType, String operationType, String content, String creator) {
        CgnciOperationLogsDO operationLogsDO = new CgnciOperationLogsDO();
        operationLogsDO.setBizNo(bizNo);
        operationLogsDO.setBizType(bizType);
        operationLogsDO.setOperationType(operationType);
        operationLogsDO.setContent(content);
        operationLogsDO.setCreator(creator);
        return operationLogsDO;
    }
}
